package Service

import (
	"context"
	"encoding/json"
	"errors"
	"net/http"
	"strconv"

	"github.com/gorilla/mux"
)

func DecodeUserRequest(c context.Context, r *http.Request) (interface{}, error) {
	vars := mux.Vars(r)
	if uid, ok := vars["uid"]; ok && uid != "" {
		uid, err := strconv.Atoi(uid)
		if err != nil {
			return nil, errors.New("参数错误!")
		}
		return UserRequest{Uid: uid, Method: r.Method}, nil
	}
	return nil, errors.New("参数错误!")
}

func EncodeUserResponse(ctx context.Context, w http.ResponseWriter, resoonse interface{}) error {
	w.Header().Set("Content-type", "application/json")
	return json.NewEncoder(w).Encode(resoonse)
}
